package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Resource
    private SpuService spuService;
    @Resource
    private SpuDetailService spuDetailService;
    @Resource
    private SkuService skuService;
    @Resource
    private SpecParamService specParamService;

    //分页查询商品
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable", required = false) Boolean saleable,
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "brandId", required = false) Long brandId,
            @RequestParam(value = "id", required = false) Long id
    ) {
        return ResponseEntity.ok(spuService.querySpuByPage(page, rows, saleable, categoryId, brandId, id));
    }

    //新增商品
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO) {
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    //商品上下架
    @PutMapping("/saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") Long id,
                                                  @RequestParam("saleable") Boolean saleable) {
        spuService.updateSpuSaleable(id, saleable);
        return ResponseEntity.ok().build();
    }

    //根据id查询商品
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable Long id) {
        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }

    //修改商品
    @PutMapping
    public ResponseEntity<SpuDTO> updateGoods(@RequestBody SpuDTO spuDTO) {
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据id查询detail
     *
     * @param id 商品id
     * @return 商品的Detail
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> querySpuDetailById(@RequestParam("id") Long id) {
        return ResponseEntity.ok(new SpuDetailDTO(spuDetailService.getById(id)));
    }


    /**
     * 根据spuID查询sku集合
     *
     * @param id spuID
     * @return sku的集合
     */
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkusBySpuId(@RequestParam("id") Long id) {
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.query().eq("spu_id", id).list()));
    }

    /**
     * 根据id批量查询sku
     *
     * @param ids id的集合
     * @return sku的集合
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam("ids") List<Long> ids) {
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.listByIds(ids)));
    }

    /**
     * 根据spuId查询spu的所有规格参数值
     *
     * @param id spu的id
     * @return 规格参数值
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecsValues(
            @RequestParam("id") Long id
            , @RequestParam(value = "searching", required = false) Boolean searching) {
        return ResponseEntity.ok(spuDetailService.querySpecValues(id, searching));
    }

    //根据商品Id查询商品Spu,不包含其它信息
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }

    /**
     * 扣减库存
     *
     * @param cartMap 商品集合
     */
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap) {
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }
}
